<template>
  <div class="cont">
    <div class="login">
      <p>注&nbsp;&nbsp;&nbsp;册</p>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="100px"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="uname">
          <el-input
            style="width: 350px"
            type="text"
            v-model="ruleForm.uname"
            placeholder="请输入汉字"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input
            style="width: 350px"
            type="password"
            v-model="ruleForm.pass"
            placeholder="请输入前两位由字母其余数字的8-15位密码"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            style="width: 350px"
            placeholder="请输入前两位由字母其余数字的8-15位密码"
            type="password"
            v-model="ruleForm.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input
            style="width: 350px"
            placeholder="请输入手机11位号"
            type="number"
            v-model="ruleForm.phone"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-button type="primary" class="btn1 btn-danger" @click="register"
        >点击注册</el-button
      >
      <div class="text">
        已经有账号了？<router-link to="/login">点击登录</router-link>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    var checkPhone = (rule, value, callback) => {
      let exp = /^1[3-9]d{9}$/;
      if (!exp.test(value)) {
        callback(new Error("请输入手机号"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("phone");
        }
        callback();
      }
    };
    var checkUname = (rule, value, callback) => {
      let exp = /^[\u4e00-\u9fa5]{0,}$/;
      if (!exp.test(value)) {
        callback(new Error("请输入汉字构成的用户名"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("uname");
        }
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      let exp = /^[a-zA-Z]\w{5,17}$/;
      if (!exp.test(value)) {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      let exp = /^[a-zA-Z]\w{5,17}$/;
      if (!exp.test(value)) {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: "",
        checkPass: "",
        uname: "",
        phone: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        uname: [{ validator: checkUname, trigger: "blur" }],
        phone: [{ validator: checkPhone, trigger: "blur" }],
      },
    };
  },

  methods: {
    register() {
      const {
        ruleForm: { pass, uname, phone },
      } = this;
      console.log(pass, uname, phone);
      const url = "http://localhost:3000/register";
      const params = `username=${uname}&password=${pass}&phone=${phone}`;
      console.log(params);
      this.axios.post(url, params).then((res) => {
        console.log(res);
        if (res.data.code == 200) {
          alert("恭喜你注册成功");
          this.$router.push("/login");
        } else {
          alert("注册失败");
        }
      });
    },
  },
};
</script>

<style  scoped src='../assets/css/07_zhuce2.css'>
</style>